package com.lili.collection.mq.executor;

import com.lili.collection.core.context.Context;
import com.lili.collection.core.executor.Executor;
import com.lili.collection.core.runner.PipeRunner;
import com.lili.collection.core.runner.result.RunnerResult;
import com.lili.collection.mq.runner.MQAsyncPipeRunner;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 *描述: 异步执行服务
 *@date:2023/3/18  16:42
 *@author:lili
 */
@Slf4j
public class AsyncExecutorService implements Executor {

    @Getter
    private Context context;

    @Getter
    @Setter
    private PipeRunner pipeRunner;

    @Override
    public void execute(Context context) {
        this.context = context;
        ensure();
        this.pipeRunner.run();
    }

    @Override
    public void setContext(Context context) {
        this.context=context;
    }

    @Override
    public Map<String, RunnerResult> getPipeRunnerResult() {
        return this.pipeRunner.getPipeRunnerResult();
    }

    private void ensure() {
        resolvePipeRunner();
    }

    private void resolvePipeRunner() {
        if (this.getPipeRunner() == null)
            this.pipeRunner = new MQAsyncPipeRunner(this.getContext());
    }

    @Override
    public String getOutputOffset() {
        ensure();
        return this.pipeRunner.getOutputOffset();
    }
}
